import { Provide, Body, Inject, Post, Get, Query } from '@midwayjs/decorator';
import { CoolController, BaseController, CoolEps } from '@cool-midway/core';
import { LoginDTO } from '../../dto/login';
import { BaseSysLoginService } from '../../service/sys/login';
import { Context } from '@midwayjs/koa';
import { Validate } from '@midwayjs/validate';

/**
 * 不需要登录的后台接口
 */
@Provide()
@CoolController()
export class BaseOpenController extends BaseController {
  @Inject()
  baseSysLoginService: BaseSysLoginService;

  @Inject()
  ctx: Context;

  @Inject()
  eps: CoolEps;

  /**
   * 实体信息与路径
   * @returns
   */
  @Get('/eps', { summary: '实体信息与路径' })
  public async getEps() {
    return this.ok(this.eps.admin);
  }

  /**
   * 登录
   * @param login
   */
  @Post('/login', { summary: '登录' })
  @Validate()
  async login(@Body() login: LoginDTO) {
    return this.ok(await this.baseSysLoginService.login(login));
  }

  /**
   * 获得验证码
   */
  @Get('/captcha', { summary: '验证码' })
  async captcha(
    @Query('type') type: string,
    @Query('width') width: number,
    @Query('height') height: number
  ) {
    return this.ok(await this.baseSysLoginService.captcha(type, width, height));
  }

  /**
   * 刷新token
   */
  @Get('/refreshToken', { summary: '刷新token' })
  async refreshToken(@Query('refreshToken') refreshToken: string) {
    return this.ok(await this.baseSysLoginService.refreshToken(refreshToken));
  }

  @Get('/analysis', { summary: '统计' })
  async analysis() {
    return this.ok({
      // 访问量
      visits: [
        { label: '1月', value: 8765 },
        { label: '2月', value: 4598 },
        { label: '3月', value: 13567 },
        { label: '4月', value: 4789 },
        { label: '5月', value: 9876 },
        { label: '6月', value: 5478 },
        { label: '7月', value: 3289 },
        { label: '8月', value: 13423 },
        { label: '9月', value: 6543 },
        { label: '10月', value: 7689 },
        { label: '11月', value: 8235 },
        { label: '12月', value: 6578 },
      ],
      // 区域排行
      regionRanking: [
        { label: '江西省', value: 90 },
        { label: '山西省', value: 90 },
        { label: '湖南省', value: 80 },
        { label: '福建省', value: 80 },
        { label: '河南省', value: 80 },
        { label: '浙江省', value: 60 },
        { label: '江苏省', value: 40 },
        { label: '四川省', value: 10 },
        { label: '广东省', value: 10 },
        { label: '上海省', value: 0 },
      ],
      // 销售额
      sales: [
        { label: '1月', current: 820, average: 620 },
        { label: '2月', current: 932, average: 712 },
        { label: '3月', current: 1002, average: 802 },
        { label: '4月', current: 901, average: 701 },
        { label: '5月', current: 954, average: 734 },
        { label: '6月', current: 934, average: 739 },
        { label: '7月', current: 1290, average: 1090 },
        { label: '8月', current: 1360, average: 1160 },
        { label: '9月', current: 1450, average: 1250 },
        { label: '10月', current: 1400, average: 1220 },
        { label: '11月', current: 1350, average: 1150 },
        { label: '12月', current: 1320, average: 1120 },
      ],
      // 访问来源
      source: [
        { label: '搜索引擎', value: 1048 },
        { label: '直接访问', value: 735 },
        { label: '邮件营销', value: 580 },
        { label: '联盟广告', value: 484 },
        { label: '视频广告', value: 300 },
      ],
    });
  }
}
